// pages/my/addCustomGoods/specSelect.js
const app = getApp()
Page({
  data: {
    type:'',
    modalOpen:false,
    modalValue:'',
    list:[],
    carTypeList:[],
    colorList:[],
    leftBtnList:[],
    secondClass:[],
    specList:[],
    searchKey:'',
    ColorSearchKey:'',
    checkedName:'',
    checkedId:'',
    page:2,
    animationData:'',
    color:false, //颜色选择器内页标识
    slideColor:false,
    colorName:'',
    secondName:'',
    secondId:'',
    colorId:'',
    classIndex:'',
    secondIndex:'',
    isOld:0,
    colorPage:2,
    listPage:2,
    classId:''//上一页传值获取的二级分类ID
  },
  onLoad: function (options) {
    let type = options.type
    // let type = 3
    this.setData({
      type //0.品牌 1.适用车型和颜色 2.类目
    })
    if (type==0) {
      this.getBrandsList(1)
      wx.setNavigationBarTitle({
        title: '选择品牌'
      })
    }else if (type==1) {
      wx.setNavigationBarTitle({
        title: '选择适用车型与颜色'
      })
      this.setData({
        brandId:options.id
      })
      this.getCarTypeList(options.id,1)
      this.getColorList(1)
    }else if (type==2) {
      let isOld = options.isOld
      wx.setNavigationBarTitle({
        title: '选择类目'
      })
      this.setData({
        isOld
      })
      this.getFirstClass()
      this.getSecondClass(2)
    }else{
      wx.setNavigationBarTitle({
        title: '填写规格选择商品颜色'
      })
      this.setData({
        classId:options.id,//8前围
        color:true
      })
      this.getSpecList()
      this.getColorList(1)
    }
  },
  onShow: function () {

  },
  //品牌列表
  getBrandsList(page){
    let data = {
      name:this.data.searchKey,
      // pageNum:page
    }
    app.request('wx_app/brand/list',data,'GET').then(res=>{
      console.log(res)
      let list = []
      if (res.code==1) {
        res.data.forEach(item=>{
          list.push(item)
        })
        this.setData({
          list
        })
      }else{
        app.errorMsg(app.message)
      }
    })
  },
  //车型列表
  getCarTypeList(bId,page){
    let data = {
      bId,
      name:this.data.searchKey,
      pageNum:page,
      pageSize:15
    }
    console.log(data)
    app.request('wx_app/mt/getMtList',data,'GET').then(res=>{
      console.log(res)
      let carTypeList = []
      if (res.code==1) {
        res.data.pageContent.forEach(item=>{
          carTypeList.push(item)
        })
        this.setData({
          carTypeList
        })
      }else{
        app.errorMsg(res.message)
      }
    })
  },
  //carType分页
  loadMoreType(){
    let page = this.data.listPage
    let data = {
      bId:this.data.brandId,
      name:this.data.searchKey,
      pageNum:page,
      pageSize:15
    }
    console.log(data)
    app.request('wx_app/mt/getMtList',data,'GET').then(res=>{
      console.log(res)
      let carTypeList = this.data.carTypeList
      if (res.code==1) {
        res.data.pageContent.forEach(item=>{
          carTypeList.push(item)
        })
        this.setData({
          carTypeList
        })
      }else{
        app.errorMsg(res.message)
      }
    })
    page++
    this.setData({
      listPage:page
    })
  },
  //获取一级分类
  getFirstClass(){
    let data = {
      parentId:0
    }
    let that = this
    app.request('wx_app/stock_manage/getSecondTypeList',data,'GET').then(res=>{
      console.log(res)
      if (res.code==1) {
        let leftBtnList = []
        res.data.forEach(item=>{
          if (this.data.isOld==1) {
            console.log(123)
            if (item.id==2||item.id==97) {
              leftBtnList.push({
                name:item.name,
                id:item.id,
                checked:0
              })
            }
          }else{
            console.log(321)
            leftBtnList.push({
              name:item.name,
              id:item.id,
              checked:0
            })
          }
        })
        this.setData({
          leftBtnList,
          checkedName:res.data[0].name,
          checkedId:res.data[0].id
        })
      }else{
        app.errorMsg(res.message)
      }
    })
  },
  //获取二级分类
  getSecondClass(parentId){
    // wx.showLoading({
    //   title:'正在加载'
    // })
    let data = {
      parentId
    }
    app.request('wx_app/stock_manage/getSecondTypeList',data,'GET').then(res=>{
      console.log(res)
      if (res.code==1) {
        let secondClass = []
        res.data.forEach(item=>{
          secondClass.push({
            name:item.name,
            id:item.id
          })
        })
        this.setData({
          secondClass,
          secondName:res.data[0].name,
          secondId:res.data[0].id
        })
        // wx.hideLoading()
      }else{
        // wx.hideLoading()
        app.errorMsg(res.message)
      }
    })
  },
  //颜色列表
  getColorList(colorPage){
    let data = {
      name:this.data.ColorSearchKey,
      pageNum:colorPage,
      pageSize:15
    }
    console.log(data)
    app.request('wx_app/color/getList',data,'GET').then(res=>{
      console.log(res)
      if (res.code==1) {
        let colorList = []
        res.data.pageContent.forEach(item=>{
          colorList.push(item)
        })
        this.setData({
          colorList
        })
      }else{
        app.errorMsg(res.message)
      }
    })
  },
  //颜色列表分页
  loadMoreColor(){
    let page = this.data.colorPage
    let data = {
      name:this.data.ColorSearchKey,
      pageNum:page,
      pageSize:15
    }
    console.log(data)
    app.request('wx_app/color/getList',data,'GET').then(res=>{
      console.log(res)
      if (res.code==1) {
        let colorList = this.data.colorList
        res.data.pageContent.forEach(item=>{
          colorList.push(item)
        })
        this.setData({
          colorList
        })
      }else{
        app.errorMsg(res.message)
      }
    })
    page++
    this.setData({
      colorPage:page
    })
  },
  //获取二级类目下规格
  getSpecList(){
    let data = {
      productType:this.data.classId
    }
    app.request('wx_app/version/list/byProductType',data,'GET').then(res=>{
      console.log(res)
      if (res.code==1) {
        let specList = []
        res.data.forEach(item=>{
          specList.push({
            name:item.name
          })
        })
        this.setData({
          specList
        })
      }else{
        app.errorMsg(res.message)
      }
    })
  },
  //添加弹窗
  bindOpenAddModal(){
    this.setData({
      modalOpen:true
    })
  },
  //弹窗确认
  bindOpenAddModalConfirm(){
    if (this.data.type==0) {
      console.log(0)
      this.addBrand()
    }else if (this.data.type==1) {
      console.log(1)
      if (this.data.color) {
        console.log('color')
        this.addColor()
      }else{
        console.log('type')
        this.addCarType()
      }
    }else if (this.data.type==3) {
      this.addColor()
    }
  },
  //品牌添加确认
  addBrand(){
    if (this.data.modalValue.length>5) {
      app.errorMsg('品牌名称请少于5个字符')
    }else{
      let data = {
        name:this.data.modalValue
      }
      app.request('wx_app/brand/save',data,'GET').then(res=>{
        console.log(res)
        if (res.code==1) {
          this.getBrandsList(1)
          this.setData({
            modalOpen:false,
            modalValue:''
          })
        }else{
          app.errorMsg(res.message)
        }
      })
    }
  },
  //适用车型添加确认
  addCarType(){
    if (this.data.modalValue.length>20) {
      app.errorMsg('车型名称请少于20个字符')
    }else{
      let data = {
        bId:this.data.brandId,
        name:this.data.modalValue
      }
      app.request('wx_app/mt/saveMt',data,'GET').then(res=>{
        console.log(res)
        if (res.code==1) {
          this.getCarTypeList(this.data.brandId,1)
          this.setData({
            modalOpen:false,
            modalValue:'',
            listPage:2
          })
        }else{
          app.errorMsg(res.message)
        }
      })
    }
  },
  //颜色添加确认
  addColor(){
    let data = {
      name:this.data.modalValue
    }
    app.request('wx_app/color/saveMtColor',data,'GET').then(res=>{
      console.log(res)
      if (res.code==1) {
        this.getColorList(1)
        this.setData({
          modalOpen:false,
          modalValue:'',
          colorPage:2
        })
      }else{
        app.errorMsg(res.message)
      }
    })
  },
  //弹窗取消
  bindOpenAddModalCancel(){
    this.setData({
      modalOpen:false,
      modalValue:''
    })
  },
  //获取弹窗内容
  getModalValue(e){
    // let value = e.detail.value
    this.setData({
      modalValue:e.detail.value.trim()
    })
  },
  //item选择
  chooseItem(e){
    let i = e.currentTarget.dataset.i
    let id = e.currentTarget.dataset.id
    let name = e.currentTarget.dataset.name
    this.setData({
      checked:i,
      checkedName:name,
      checkedId:id
    })
    if(this.data.type==1){
      this.slideColor()
    }
  },
  //一级分类选择
  bindTapLeftBtn(e){
    let index = e.currentTarget.dataset.index
    let id = e.currentTarget.dataset.id
    let name =e.currentTarget.dataset.name
    this.setData({
      classIndex:index,
      checkedName:name,
      checkedId:id,
      secondIndex:''
    })
    this.getSecondClass(id)
  },
  //二级类目选择
  bindSecondClassChoose(e){
    let index = e.currentTarget.dataset.index
    let name = e.currentTarget.dataset.name
    let id = e.currentTarget.dataset.id
    this.setData({
      secondIndex:index,
      secondName:name,
      secondId:id
    })
  },
  //color选择
  chooseColor(e){
    let i = e.currentTarget.dataset.i
    let name = e.currentTarget.dataset.name
    let id = e.currentTarget.dataset.id
    this.setData({
      colorCheck:i,
      colorName:name,
      colorId:id
    })
    this.slideOutColor()
  },
  //获取搜索
  getSearchKey(e){
    this.setData({
      searchKey:e.detail.value
    })
  },
  //获取颜色搜索
  getColorSearchKey(e){
    this.setData({
      ColorSearchKey:e.detail.value
    })
  },
  //确认搜索
  confirmSearch(){
    if (this.data.type==0) {
      this.getBrandsList(1)
    }else if (this.data.type==1) {
      if (this.data.color) {
        this.getColorList(1)
      }else{
        this.getCarTypeList(this.data.brandId,1)
      }
    }else if (this.data.type==3) {
      this.getColorList(1)
    }
  },
  //确认选择
  confirmChoose(){
    let pages = getCurrentPages()
    let prev = pages[pages.length - 2]
    if (this.data.type==0) {
      prev.setData({
        brand:this.data.checkedName,
        brandId:this.data.checkedId
      })
      wx.navigateBack({
        delta:1
      })
    }else if (this.data.type==1) {
      prev.setData({
        carType:this.data.checkedName,
        carTypeId:this.data.checkedId,
        carTypeColorName:this.data.colorName,
        carTypeColorId:this.data.colorId
      })
      wx.navigateBack({
        delta:1
      })
    }else if (this.data.type==2) {
      if (this.data.checkedId==2) {
        prev.setData({
          specMark:false
        })
      }else{
        prev.setData({
          specMark:true
        })
      }
      prev.setData({
        firstClassName:this.data.checkedName,
        firstClassId:this.data.checkedId,
        secondClassName:this.data.secondName,
        secondClassId:this.data.secondId
      })
      wx.navigateBack({
        delta:1
      })
    }else{
      if (this.data.checkedName==''||this.data.colorName=='') {
        app.errorMsg('请输入规格内容和选择颜色')
      }else{
        let data = {
          productType:this.data.classId,
          name:this.data.checkedName
        }
        console.log(data)
        app.request('wx_app/version/save',data,'GET').then(res=>{
          console.log(res)
          if (res.code==1) {
            prev.setData({
              specName:this.data.checkedName,
              specColorName:this.data.colorName,
              specColorId:this.data.colorId,
              specId:res.data.id
            })
            wx.navigateBack({
              delta:1
            })
          }else{
            app.errorMsg(res.message)
          }
        })
      }
    }
  },
  //获取自定义规格
  getSpecValue(e){
    this.setData({
      checkedName:e.detail.value
    })
  },
  //划入颜色选择
  slideColor(){
    let animation = wx.createAnimation({
      duration:200,
      timingFunction:'ease-out',
      delay:0
    })
    animation.left('0').step();
    this.setData({
      animationData: animation.export(),
      slideColor:true,
      color:true
    })
  },
  //移出颜色选择
  slideOutColor(){
    let animation = wx.createAnimation({
      duration:500,
      timingFunction:'ease-out',
      delay:0
    })
    animation.left('100%').step();
    this.setData({
      animationData: animation.export(),
      slideColor:false,
      color:false
    })
  },
})